//
//  AppDelegate.m
//  ScoreGame
//
//  Created by 黄裕杰 on 16/8/19.
//  Copyright © 2016年 黄裕杰. All rights reserved.
//


#import "AppDelegate.h"

// *** 友盟推送
#import "UMessage.h"

#import "MobClick.h"

//微信SDK头文件
#import "WXApi.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>

#import "LBLaunchImageAdView.h"
#import "ATMCoreServer.h"

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "ATMInfoHelper.h"
#import "SSDKUserData.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _initShareSDK();
    _initUMSDKKey(launchOptions);
    
    [self.window makeKeyAndVisible];
    [ATMCoreServer startup];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    
    //设置显示的颜色
    
    bar.barTintColor = [UIColor colorWithRed:79/255. green:178/255. blue:253/255. alpha:1];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    bar.tintColor = [UIColor whiteColor];
    
    return YES;
}

static void _initShareSDK(){
    
    [ShareSDK registerApp:ShareSDKAppKey activePlatforms:@[@(SSDKPlatformTypeWechat),@(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType){
                     switch (platformType){
                         case SSDKPlatformTypeWechat:
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         case SSDKPlatformTypeQQ:
                             [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                             break;
                         default:
                             break;
                     }
                 }onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo){
                     switch (platformType){
                         case SSDKPlatformTypeWechat:
                             [appInfo SSDKSetupWeChatByAppId:WXDevelopmentAppKey appSecret:WXDevelopmentAppSecret];
                             break;
                         case SSDKPlatformTypeQQ:
                             [appInfo SSDKSetupQQByAppId:QQDevelopmentAppKey appKey:QQDevelopmentAppSecret authType:SSDKAuthTypeSSO];
                             break;
                         default:
                             break;
                     }
                 }];
}

#pragma mark - 初始化友盟KEY
static void _initUMSDKKey(NSDictionary *launchOptions){
    
    // *** 统计
    [MobClick startWithAppkey:UMSDKAppKey reportPolicy:BATCH   channelId:@"App Store"];
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];
    
    
    // *** 友盟推送
    [UMessage startWithAppkey:UMSDKAppKey launchOptions:launchOptions];
    
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    action1.identifier = @"action1_identifier";
    action1.title=@"Accept";
    action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
    
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
    action2.identifier = @"action2_identifier";
    action2.title=@"Reject";
    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序，在后台处理
    action2.authenticationRequired = YES;//需要解锁才能处理，如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略；
    action2.destructive = YES;
    
    UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    actionCategory.identifier = @"category1";//这组动作的唯一标示
    [actionCategory setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
    
    NSSet *categories = [NSSet setWithObject:actionCategory];
    
    //如果默认使用角标，文字和声音全部打开，请用下面的方法
    [UMessage registerForRemoteNotifications:categories];
}

#pragma mark - 消息推送
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
    [UMessage registerDeviceToken:deviceToken];
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    NSString *tokenStr = [deviceToken description];
    // *** 获取去掉空格和尖括号后的token
    NSString *pushToken = [[[tokenStr
                             stringByReplacingOccurrencesOfString:@"<" withString:@""]
                            stringByReplacingOccurrencesOfString:@">" withString:@""]
                           stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"token:%@",pushToken);
    [[NSUserDefaults standardUserDefaults] setObject:pushToken forKey:@"apnsToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
    NSLog(@"Failed to get token, error: %@", error);
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    // Register to receive notifications.
    [application registerForRemoteNotifications];
}
#endif

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{
    // Handle the actions.
    if ([identifier isEqualToString:@"declineAction"]){
        
    }else if ([identifier isEqualToString:@"answerAction"]){
        
    }
}

@end
